#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/9/12 16:06
# @Author  : 王凯
# @File    : gdzc_claen.py
# @Project : scrapy_spider
import datetime
import json
from pathlib import Path

from apps.data_stats.data_stats.clean import BaseClean, CODE_MAPPING
from apps.patent.clean import IndustryModel


class GdzcClaen(BaseClean):

    def __init__(self):
        super().__init__()
        with open(f"{Path(__file__).parent.parent.parent.parent.parent}/apps/patent/clean/dm_gjhyhf_map_qyzhpjhyysb.json", "r", encoding="utf-8") as f:
            self.industry_model = IndustryModel(industry_info=json.loads(f.read()))

    def save_data(self, datas):
        batch_size = 1000
        for i in range(0, len(datas), batch_size):
            self.to_db.add_batch_smart("net_industry_fixed_assets_investmen", list(datas[i:i + batch_size]), update_columns=list(datas[0].keys()))

    def deal_df(self, df):
        df["industry_name"] = df["tag_name"].map(lambda x: x.split("固定资产投资额_")[0])
        df['industry_code'] = df["industry_name"].map(lambda x: CODE_MAPPING.get(x) or self.industry_model.search_maps.get(x))
        df['fixed_assets_investment_sum_yoy_growthrate'] = df["num"].map(lambda x: x / 100)
        df['time'] = df["date_string"]
        datas = df[['time', 'industry_code', 'industry_name', 'fixed_assets_investment_sum_yoy_growthrate']].drop_duplicates().to_dict('records')
        self.save_data(datas)
        return datas

    def run_2004(self):
        df = self.get_data_from_db(cate_1="固定资产投资(不含农户)", cate_2="分行业固定资产投资情况(2004-2011)", condition=" and area = '全国' ")
        df = df[df['date_string'].str.startswith(("2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011"))]
        datas = self.deal_df(df)
        return datas

    def run_2012(self):
        df = self.get_data_from_db(cate_1="固定资产投资(不含农户)", cate_2="分行业固定资产投资情况(2012-2017)", condition=" and area = '全国' ")
        df = df[df['date_string'].str.startswith(("2012", "2013", "2014", "2015", "2016", "2017"))]
        datas = self.deal_df(df)
        return datas

    def run_2018(self):
        df = self.get_data_from_db(cate_1="固定资产投资(不含农户)", cate_2="按行业分固定资产投资增速（2018-）", condition=" and area = '全国' ")
        df = df[df['date_string'].str.startswith(tuple(str(i) for i in range(2018, datetime.datetime.now().year + 1)))]
        datas = self.deal_df(df)
        return datas

    def run(self):
        self.run_2004()
        self.run_2012()
        self.run_2018()


if __name__ == "__main__":
    GdzcClaen().run()
